<template>
  <div class="default-table-detail-container">
    <el-page-header :content="can_edit ? '编辑' : '详情'" @back="goBack" @click="edit" />

    <el-descriptions border :column="column" :title="config_table.table_comment + '详情'">
      <!-- 自定义 -->
      <template v-for="(item, index) in config_table.cols" :key="index">
        <el-descriptions-item v-if="item.field != 'uniacid' && item.field != 'is_del' && !hide_field_arr.includes(item.field)">
          <template #label>{{ item.comment }}</template>
          {{
            item.model && form[item.model]
              ? form[item.model][item.show_field]
              : item.field == 'status'
                ? form.show_status
                : form[item.field]
          }}
        </el-descriptions-item>
      </template>
      <!-- 自定义 -->
    </el-descriptions>
    <obj-edit ref="editRef" :rules="rules" :span="edit_span" :width="edit_width" @fetch-data="detailData" />
  </div>
</template>

<script lang="ts" setup>
import { api_detail } from '/@/api/obj_base'
import { useRoutesStore } from '/@/store/modules/routes'
import { useTabsStore } from '/@/store/modules/tabs'
import { handleActivePath } from '/@/utils/routes'
defineOptions({
  name: 'ObjDetail',
})

const route: any = useRoute()
const router = useRouter()
const $pub = inject<any>('$pub')
const tabsStore = useTabsStore()
const { changeTabsMeta, delVisitedRoute } = tabsStore
const form = reactive<any>({ text: '' })
const config_table = reactive<any>({
  table_comment: '',
})
const routesStore = useRoutesStore()
const tabStore = useTabsStore()
const { getVisitedRoutes: visitedRoutes } = storeToRefs(tabStore)
const { changeActiveMenu } = routesStore
const rate = ref<number>(parseInt(route.query.rate))
const column = reactive<any>(parseInt(route.query.column))
const showid = reactive<any>(parseInt(route.query.showid))
const can_edit = reactive<any>(parseInt(route.query.can_edit))
const id = reactive<any>(parseInt(route.query.id))
const tablename = reactive<any>(route.query.t)
const hide_field_arr = reactive<any>([])

//设置编辑页
const edit_width = '518px'
const edit_span = 20
//设置必填项
const rules = reactive<any>({
  name: [{ required: true, trigger: 'blur', message: '请输入名称' }],
})

const editRef = ref<any>(null)

//通用编辑
const edit = () => {
  if (can_edit) {
    api_detail(config_table.tablename, { id: parseInt(id) }).then((res: any) => {
      if (res.detail) {
        editRef.value.showEdit(res)
      }
    })
  }
}

const isActive = (path: any) => {
  return path === handleActivePath(route, true)
}
const toLastTab = async () => {
  const latestView = visitedRoutes.value.filter((item) => item.path !== handleActivePath(route, true)).slice(-1)[0]
  if (latestView) await router.push(latestView)
  else await router.push('/')
}
const handleTabRemove = async (rawPath: string) => {
  if (isActive(rawPath)) await toLastTab()
  await delVisitedRoute(rawPath)
}

const handleActiveMenu = (activeMenu: string) => {
  changeActiveMenu(activeMenu)
}

const detailData = async () => {
  console.log(id)
  if (!tablename) {
    handleTabRemove(route.path)
    return false
  }
  api_detail(tablename, { id: id }).then((res: any) => {
    Object.assign(config_table, res.config_table)
    console.log(res.config_table)
    Object.assign(form, res.detail)

    const hide_field = route.query.hide_field

    //隐藏字段的数组
    Object.assign(hide_field_arr, hide_field.split(','))

    console.log(res.detail)
    changeTabsMeta({
      title: '详情页',
      meta: {
        title: `${config_table.table_comment}详情 - ${route.query.id}`,
      },
    })
  })

  const acmenu = `/application/obj?t=${tablename}`
  console.log('acmenu', `${acmenu}`)
  handleActiveMenu(`${acmenu}`)
}

const goBack = async () => {
  await delVisitedRoute(handleActivePath(route, true))
  history.back()
}

onMounted(() => {
  detailData()
})
</script>

<style lang="scss" scoped>
.default-table-detail-container {
  :deep() {
    .el-form--inline {
      .el-form-item {
        margin-right: 10px;
      }
    }

    .el-descriptions__label {
      min-width: 80px !important;
      text-align: right;
    }
  }
}
</style>
